package top.coolrainly.module.resource.controller.admin.content;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import top.coolrainly.framework.common.pojo.PageParam;
import top.coolrainly.framework.common.pojo.PageResult;
import top.coolrainly.framework.common.pojo.CommonResult;
import top.coolrainly.framework.common.util.object.BeanUtils;
import static top.coolrainly.framework.common.pojo.CommonResult.success;

import top.coolrainly.framework.excel.core.util.ExcelUtils;

import top.coolrainly.framework.apilog.core.annotation.ApiAccessLog;
import static top.coolrainly.framework.apilog.core.enums.OperateTypeEnum.*;

import top.coolrainly.module.resource.controller.admin.content.vo.*;
import top.coolrainly.module.resource.dal.dataobject.content.ResourceContentDO;
import top.coolrainly.module.resource.service.content.ResourceContentService;

@Tag(name = "管理后台 - Web3D 资源 内容")
@RestController
@RequestMapping("/resource/content")
@Validated
public class ResourceContentController {

    @Resource
    private ResourceContentService contentService;

    @PostMapping("/create")
    @Operation(summary = "创建Web3D 资源 内容")
    @PreAuthorize("@ss.hasPermission('resource:content:create')")
    public CommonResult<Long> createContent(@Valid @RequestBody ResourceContentSaveReqVO createReqVO) {
        return success(contentService.createContent(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新Web3D 资源 内容")
    @PreAuthorize("@ss.hasPermission('resource:content:update')")
    public CommonResult<Boolean> updateContent(@Valid @RequestBody ResourceContentSaveReqVO updateReqVO) {
        contentService.updateContent(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除Web3D 资源 内容")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('resource:content:delete')")
    public CommonResult<Boolean> deleteContent(@RequestParam("id") Long id) {
        contentService.deleteContent(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得Web3D 资源 内容")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('resource:content:query')")
    public CommonResult<ResourceContentRespVO> getContent(@RequestParam("id") Long id) {
        ResourceContentDO content = contentService.getContent(id);
        return success(BeanUtils.toBean(content, ResourceContentRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得Web3D 资源 内容分页")
    @PreAuthorize("@ss.hasPermission('resource:content:query')")
    public CommonResult<PageResult<ResourceContentRespVO>> getContentPage(@Valid ResourceContentPageReqVO pageReqVO) {
        PageResult<ResourceContentDO> pageResult = contentService.getContentPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ResourceContentRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出Web3D 资源 内容 Excel")
    @PreAuthorize("@ss.hasPermission('resource:content:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportContentExcel(@Valid ResourceContentPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ResourceContentDO> list = contentService.getContentPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "Web3D 资源 内容.xls", "数据", ResourceContentRespVO.class,
                        BeanUtils.toBean(list, ResourceContentRespVO.class));
    }

    @GetMapping("/list")
    @Operation(summary = "获得Web3D 资源 内容列表")
    @Parameter(name = "type", description = "类型")
    @PreAuthorize("@ss.hasPermission('resource:group:query')")
    public CommonResult<List<ResourceContentRespVO>> getContentList(@RequestParam(value = "type", required = false) Integer type) {
        return success(BeanUtils.toBean(contentService.getContentList(type), ResourceContentRespVO.class));
    }

    @GetMapping("/get-by-group")
    @Operation(summary = "获得Web3D 资源 内容，根据groupId获取")
    @PreAuthorize("@ss.hasPermission('resource:content:query')")
    public CommonResult<List<ResourceContentRespVO>> getContentByGroupId(@RequestParam("groupId") Long groupId) {
        return success(BeanUtils.toBean(contentService.getContentListByGroupId(groupId), ResourceContentRespVO.class));
    }

}